from django.conf import settings
import Pyro.core
import sys

def _getServer():
    global server
    server = Pyro.core.getProxyForURI(settings.QUEUESERVER_URL)
    return server

def delay_task(task, **kwargs):
    print >>sys.stderr, "delay_task", task, kwargs
    server = _getServer()
    message = kwargs
    message['task'] = task
    server.push(message, settings.QUEUESERVER_LOGIN)
    print >>sys.stderr, "delay_task - OK", task, kwargs

def retryMessage(message):
    if not "retries" in message:
        message["retries"] = 1
    else:
        message["retries"] += 1
    if message["retries"] > settings.MAX_RETRIES:
        print >>sys.stderr, "retry limit exceded:", message
        return
    server = _getServer()
    server.push(message, settings.QUEUESERVER_LOGIN)
